﻿using System;
using System.Text.RegularExpressions;

namespace wojilu
{
    /// <summary>
    /// 数据类型转移
    /// </summary>
    public static class TypeCon
    {
        #region string型转换为bool型

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(this object expression, bool defValue)
        {
            if (expression != null)
                return StrToBool(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 转换对象为布尔值
        /// </summary>
        /// <param name="Value">对象</param>
        /// <returns>转换后的布尔值</returns>
        public static bool StrToBool1(object Value)
        {
            if (!PageValidator.IsNull(Value) && PageValidator.IsBool(Value))
            {
                string[] array = new string[] { "true", "yes", "1" };
                return (Array.IndexOf<string>(array, Value.ToString().ToLower()) >= 0);
            }
            return false;
        }

        /// <summary>
        /// 转换对象为布尔值
        /// </summary>
        /// <param name="Value">对象</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns>转换后的布尔值</returns>
        public static bool StrToBool1(object Value, bool DefaultValue)
        {
            if (!PageValidator.IsNull(Value) && PageValidator.IsBool(Value))
            {
                string[] array = new string[] { "true", "yes", "1" };
                return ((Array.IndexOf<string>(array, Value.ToString().ToLower()) >= 0) || DefaultValue);
            }
            return DefaultValue;
        }

        /// <summary>
        /// string型转换为bool型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool StrToBool(string expression, bool defValue)
        {
            if (expression != null)
            {
                if (string.Compare(expression, "true", true) == 0)
                    return true;
                else if (string.Compare(expression, "false", true) == 0)
                    return false;
            }
            return defValue;
        }
        #endregion

        #region 转换对象为字符串
        /// <summary>
        /// 转换对象为字符串
        /// </summary>
        /// <param name="Value">对象</param>
        /// <returns>转换后的字符串</returns>
        public static string ToString(object Value)
        {
            if (PageValidator.IsNull(Value))
            {
                return string.Empty;
            }
            return Value.ToString();
        }

        /// <summary>
        /// 转换对象为字符串
        /// </summary>
        /// <param name="Value">对象</param>
        /// <param name="DefaultValue">默认值</param>
        /// <param name="Trim">是否去除空格</param>
        /// <returns>转换后的字符串</returns>
        public static string ToString(object Value, string DefaultValue, bool Trim)
        {
            if (PageValidator.IsNull(Value))
            {
                return ToString(DefaultValue);
            }
            if (Trim)
            {
                return Value.ToString().Trim();
            }
            return Value.ToString();
        }

        #endregion

        #region 将对象转换为Int32类型
        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(this object expression)
        {
            return ObjectToInt(expression, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int ObjectToInt(this object expression, int defValue)
        {
            if (expression != null)
                return StrToInt(expression.ToString(), defValue);

            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型,转换失败返回0
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(this string str)
        {
            return StrToInt(str, 0);
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(string str, int defValue)
        {
            if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;

            int rv;
            if (Int32.TryParse(str, out rv))
                return rv;

            return Convert.ToInt32(StrToFloat(str, defValue));
        }
        #endregion

        #region string型转换为float型
        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(object strValue, float defValue)
        {
            if ((strValue == null))
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float ObjectToFloat(object strValue, float defValue)
        {
            if ((strValue == null))
                return defValue;

            return StrToFloat(strValue.ToString(), defValue);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float ObjectToFloat(object strValue)
        {
            return ObjectToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue)
        {
            if ((strValue == null))
                return 0;

            return StrToFloat(strValue.ToString(), 0);
        }

        /// <summary>
        /// string型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
                return defValue;

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                    float.TryParse(strValue, out intValue);
            }
            return intValue;
        }
        #endregion

        #region 将对象转换为日期时间类型
        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime StrToDateTime(string str, DateTime defValue)
        {
            if (!string.IsNullOrEmpty(str))
            {
                DateTime dateTime;
                if (DateTime.TryParse(str, out dateTime))
                    return dateTime;
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime StrToDateTime(string str)
        {
            return StrToDateTime(str, DateTime.Now);
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime ObjectToDateTime(object obj)
        {
            return StrToDateTime(obj.ToString());
        }

        /// <summary>
        /// 将对象转换为日期时间类型
        /// </summary>
        /// <param name="obj">要转换的对象</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static DateTime ObjectToDateTime(object obj, DateTime defValue)
        {
            return StrToDateTime(obj.ToString(), defValue);
        }
        #endregion

        #region 字符串转成整型数组
        /// <summary>
        /// 字符串转成整型数组
        /// </summary>
        /// <param name="idList">要转换的字符串</param>
        /// <returns>转换后的int类型结果</returns>
        public static int[] StringToIntArray(string idList)
        {
            return StringToIntArray(idList, -1);
        }

        /// <summary>
        /// 字符串转成整型数组
        /// </summary>
        /// <param name="idList">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int[] StringToIntArray(string idList, int defValue)
        {
            if (string.IsNullOrEmpty(idList))
                return null;
            string[] strArr = StringArray.SplitString(idList, ",");
            int[] intArr = new int[strArr.Length];
            for (int i = 0; i < strArr.Length; i++)
                intArr[i] = StrToInt(strArr[i], defValue);

            return intArr;
        }
        #endregion

        #region 转换为Decimal
        /// <summary>
        /// object转换为Decimal
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <returns>返回Decimal</returns>
        public static decimal CDecimal(object input)
        {
            return CDecimal(input, 0M);
        }

        /// <summary>
        /// string转换为Decimal
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>返回Decimal</returns>
        public static decimal CDecimal(string input)
        {
            return CDecimal(input, 0M);
        }

        /// <summary>
        /// object转换为Decimal
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Decimal</returns>
        public static decimal CDecimal(object input, decimal defaultValue)
        {
            if (!PageValidator.IsNull(input))
            {
                return CDecimal(input.ToString(), defaultValue);
            }
            return 0M;
        }

        /// <summary>
        /// string转换为Decimal
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Decimal</returns>
        public static decimal CDecimal(string input, decimal defaultValue)
        {
            decimal num;
            if (!decimal.TryParse(input, out num))
            {
                num = defaultValue;
            }
            return num;
        }

        #endregion

        #region 转换为Double
        // <summary>
        /// object转换为Double
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <returns>返回Double</returns>
        public static double CDouble(object input)
        {
            return CDouble(input, 0.0);
        }

        /// <summary>
        /// object转换为Double
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Double</returns>
        public static double CDouble(object input, double defaultValue)
        {
            if (!PageValidator.IsNull(input))
            {
                return CDouble(input.ToString(), defaultValue);
            }
            return 0.0;
        }

        /// <summary>
        /// string转换为Double
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>返回Double</returns>
        public static double CDouble(string input)
        {
            return CDouble(input, 0.0);
        }

        /// <summary>
        /// string转换为Double
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Double</returns>
        public static double CDouble(string input, double defaultValue)
        {
            double num;
            if (!double.TryParse(input, out num))
            {
                return defaultValue;
            }
            return num;
        }


        #endregion

        #region 转换为Long类型
        /// <summary>
        /// object转换为Long类型
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <returns>返回Long类型</returns>
        public static int CLng(object input)
        {
            return CLng(input, 0);
        }

        /// <summary>
        /// object转换为Long类型
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Long类型</returns>
        public static int CLng(object input, int defaultValue)
        {
            if (!PageValidator.IsNull(input))
            {
                return CLng(input.ToString(), defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// string转换为Long类型
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>返回Long类型</returns>
        public static int CLng(string input)
        {
            return CLng(input, 0);
        }

        /// <summary>
        /// string转换为Long类型
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Long类型</returns>
        public static int CLng(string input, int defaultValue)
        {
            int num;
            if (!int.TryParse(input, out num))
            {
                num = defaultValue;
            }
            return num;
        }

        #endregion

        #region 转换为Single
        /// <summary>
        /// object转换为Single
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <returns>返回Single</returns>
        public static float CSingle(object input)
        {
            return CSingle(input, 0f);
        }

        /// <summary>
        /// object转换为Single
        /// </summary>
        /// <param name="input">要转换的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Single</returns>
        public static float CSingle(object input, float defaultValue)
        {
            if (!PageValidator.IsNull(input))
            {
                return CSingle(input.ToString(), defaultValue);
            }
            return 0f;
        }

        /// <summary>
        /// string转换为Single
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <returns>返回Single</returns>
        public static float CSingle(string input)
        {
            return CSingle(input, 0f);
        }

        /// <summary>
        /// string转换为Single
        /// </summary>
        /// <param name="input">要转换的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回Single</returns>
        public static float CSingle(string input, float defaultValue)
        {
            float num;
            if (!float.TryParse(input, out num))
            {
                num = defaultValue;
            }
            return num;
        }

        #endregion

        #region 转换对象为整形数值
        /// <summary>
        /// 转换对象为整形数值
        /// </summary>
        /// <param name="Value">对象</param>
        /// <returns>转换后的整形数值</returns>
        public static int ToInt(object Value)
        {
            return ToInt(Value, 0);
        }

        /// <summary>
        /// 转换对象为整形数值
        /// </summary>
        /// <param name="Value">对象</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns>转换后的整形数值</returns>
        public static int ToInt(object Value, int DefaultValue)
        {
            if (!PageValidator.IsNull(Value) && PageValidator.IsInt(Value))
            {
                return int.Parse(Value.ToString());
            }
            return DefaultValue;
        }

        #endregion

        #region 转换对象为长整形数值
        /// <summary>
        /// 转换对象为长整形数值
        /// </summary>
        /// <param name="Value">对象</param>
        /// <returns>转换后的长整形数值</returns>
        public static long ToLong(object Value)
        {
            return ToLong(Value, 0L);
        }

        /// <summary>
        /// 转换对象为长整形数值
        /// </summary>
        /// <param name="Value">对象</param>
        /// <param name="DefaultValue">默认值</param>
        /// <returns>转换后的长整形数值</returns>
        public static long ToLong(object Value, long DefaultValue)
        {
            if (!PageValidator.IsNull(Value) && PageValidator.IsInt(Value))
            {
                return long.Parse(Value.ToString());
            }
            return DefaultValue;
        }
        #endregion

        #region 转换对象为日期值

        /// <summary>
        /// 转换为日期
        /// </summary>
        /// <param name="input">对象</param>
        /// <returns>转换后的日期值</returns>
        public static DateTime CDate(object input)
        {
            if (!PageValidator.IsNull(input))
            {
                return CDate(input.ToString());
            }
            return DateTime.Now;
        }

        /// <summary>
        /// 转换为日期
        /// </summary>
        /// <param name="input">日期型字符串</param>
        /// <returns>转换后的日期值</returns>
        public static DateTime CDate(string input)
        {
            DateTime now;
            if (!DateTime.TryParse(input, out now))
            {
                now = DateTime.Now;
            }
            return now;
        }

        /// <summary>
        /// 转换为日期
        /// </summary>
        /// <param name="input">日期型字符串</param>
        /// <param name="outTime">默认值</param>
        /// <returns>转换后的日期值</returns>
        public static DateTime? CDate(string input, DateTime? outTime)
        {
            DateTime time;
            if (!DateTime.TryParse(input, out time))
            {
                return outTime;
            }
            return new DateTime?(time);
        }

        #endregion

        #region int型转换为string型
        /// <summary>
        /// int型转换为string型
        /// </summary>
        /// <returns>转换后的string类型结果</returns>
        public static string IntToStr(int intValue)
        {
            return Convert.ToString(intValue);
        }
        #endregion

        #region 将long型数值转换为Int32类型
        /// <summary>
        /// 将long型数值转换为Int32类型
        /// </summary>
        /// <param name="objNum"></param>
        /// <returns></returns>
        public static int SafeInt32(object objNum)
        {
            if (objNum == null)
                return 0;

            string strNum = objNum.ToString();
            if (PageValidator.IsNumeric(strNum))
            {

                if (strNum.ToString().Length > 9)
                {
                    if (strNum.StartsWith("-"))
                        return int.MinValue;
                    else
                        return int.MaxValue;
                }
                return Int32.Parse(strNum);
            }
            else
                return 0;
        }
        #endregion


    }
}
